package com.cloud.service.Impl;

import com.cloud.dao.ApplyTestDao;
import com.cloud.domain.LoginUser;
import com.cloud.myInterface.Factory;
import com.cloud.pojo.UserApplyTest;
import com.cloud.pojo.UserApplyTestVue;
import com.cloud.service.ApplyTestService;
import com.cloud.pojo.ApplyTest;
import com.cloud.tools.CommonResult;
import com.cloud.tools.CommonResultFactory;
import com.cloud.utils.MyTime;
import com.cloud.utils.QRCodeUtil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： linJianHong
 * @dateTime： 21:51 2022/4/17
 * @Description:
 * @ClassName:
 */

@Service
public class ApplyTestServiceImpl implements ApplyTestService {

    @Resource
    ApplyTestDao applyTestDao;

    private final Factory resultFactory = new CommonResultFactory();

    @Override
    public CommonResult create() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = loginUser.getUser().getUserId();
        int testLocation = loginUser.getUser().getDefaultRegion();

//        String userId = "001";
//        int testLocation = 1;
        String nowTime = MyTime.getCurrTimeAsString();

        String content = userId + "$" +testLocation + "$" + nowTime;
        String qrCode = QRCodeUtil.zxingCodeCreate(content, "E:\\dasanxia\\QRcode\\"+nowTime.substring(0, 10)+"\\",
                250, null);

        ApplyTest applyTest = new ApplyTest();
        applyTest.setUserId(userId);
        applyTest.setTestLocation(testLocation);
        applyTest.setQrCode(qrCode);
        applyTest.setApplyTime(MyTime.getCurrTimeAsDate());

        if(applyTestDao.create(applyTest) == 1){
            return resultFactory.getResultInstance(200,"核酸检测申请成功",1);
        }
        return resultFactory.getResultInstance(200,"核酸检测申请失败，稍后重试",0);

    }

    public static String getImgStr(String imgFilePath) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        File file=new File(imgFilePath);
        InputStream in;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    @Override
    public CommonResult retrieve() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = loginUser.getUser().getUserId();

//        String userId = "001";
        List<UserApplyTest> res1 = applyTestDao.retrieve(userId);
        List<UserApplyTestVue> res2 = new ArrayList<>();
        for(UserApplyTest item: res1){
            UserApplyTestVue userApplyTestVue = new UserApplyTestVue();
            userApplyTestVue.setTestLocation(item.getTestLocation());
            userApplyTestVue.setQrCode(getImgStr(item.getQrCode().replaceAll("\n", "")));
            res2.add(userApplyTestVue);
        }
        if(res2.size() >= 1){
            return resultFactory.getResultInstance(200,"核酸检测申请查看成功",res2);
        }
        return resultFactory.getResultInstance(200,"暂无核酸检测申请记录",null);
    }
}
